package com.miyake.loveviewweb.client.community;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.dialog.DialogHandler;
import com.miyake.loveviewweb.client.dialog.IDialogPanel;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.client.viewer.AbstractListWidget;
import com.miyake.loveviewweb.shared.model.GroupModel;
import com.miyake.loveviewweb.shared.model.UserModel;

public class GroupListWidget extends AbstractListWidget<GroupModel> implements IDialogPanel {
    
    public GroupListHandler getHandler() {
        return handler;
    }

    public void setHandler(GroupListHandler handler) {
        this.handler = handler;
    }
    
    private IGroupList.GroupListType type;
    private GroupListHandler handler;
    private DialogHandler dialogHandler;
    
    protected void updateSuper() {
        super.update();
    }
    
    @Override
    public void update() {
        if (type.equals(IGroupList.GroupListType.AllGroup)) {
            DataService.Util.getInstance().getAllGroups(new AsyncCallback<List<GroupModel>>() {
    
                public void onFailure(Throwable caught) {
   
                }
    
                public void onSuccess(List<GroupModel> result) {
                    setElements(result);
                    updateSuper();
                }
                
            });
        }
        else if (type.equals(IGroupList.GroupListType.MyGroup)) {
            DataService.Util.getInstance().getMyGroups(new AsyncCallback<List<GroupModel>>() {

                public void onFailure(Throwable caught) {

                }

                public void onSuccess(List<GroupModel> result) {
                    setElements(result);
                    updateSuper();
                }
                
            });
        }
    }

    public enum GroupListType {
        AllGroup, MyGroup
    }
    
    public GroupListWidget(IGroupList.GroupListType type, GroupListHandler handler) {
        this.handler = handler;
        initialize(type);
    }

    public GroupListWidget(IGroupList.GroupListType type) {
        initialize(type);
    }
    
    private void initialize(IGroupList.GroupListType type) {
        this.type = type;
        getGrid().setBorderWidth(1);
        setColumns(6);
    }

    @Override
    protected Widget getWidget(final GroupModel element) {
        VerticalPanel panel = new VerticalPanel();
        
        Anchor anchor = new Anchor(element.getName());
        panel.add(anchor);
        panel.add(new Label(element.getDescription()));
        
        final Anchor owner = new Anchor();
        panel.add(owner);
        anchor.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                handler.onSelect(element);
                if (dialogHandler != null)  {
                    dialogHandler.onOK();
                }
            }
            
        });
        
        DataService.Util.getInstance().getUser(element.getOwner(), new AsyncCallback<UserModel>(){
            public void onFailure(Throwable caught) {  
            }

            public void onSuccess(UserModel result) {
                owner.setText(getOwnerString(result));
            } 
        });
        return panel;
    }

    protected String getOwnerString(UserModel result) {
        return result.getFirstName() + "," + result.getLastName();
    }

    public void setGroupListHandler(GroupListHandler handler) {
        this.handler = handler;
    }

    public Composite getComposite() {
        return this;
    }

    public void setDialogHandler(DialogHandler handler) {
        this.dialogHandler = handler;
    }
}
